package com.hongdee.scm.invioce.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.car.entity.TScmCar;
import com.hongdee.scm.invioce.dao.ITCrmCarInvoiceDao;
import com.hongdee.scm.invioce.entity.TCrmCarInvoice;
import com.hongdee.scm.invioce.service.ITCrmCarInvoiceService;
import com.hongdee.scm.saleorder.entity.TScmSaleOrder;
import com.hongdee.scm.saleorder.entity.TScmSaleOrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TCrmCarInvoiceServiceImpl
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.crm.invioce.service.impl
 * 作  者: 术洪超
 * 日  期:2017-07-04 20:57
 */
@Service
public class TCrmCarInvoiceServiceImpl implements ITCrmCarInvoiceService {

    @Autowired
    private ITCrmCarInvoiceDao itCrmCarInvoiceDao;
    @Autowired
    private IHibernateDao iHibernateDao;

    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加的实体内容
     * @author 术洪超
     */

    @Override
    public boolean saveOrUpdate(TCrmCarInvoice data, String uid) {
        try {
            data.setCreateUser(uid);
            data.setCreateTime(Calendar.getInstance().getTimeInMillis());
            this.itCrmCarInvoiceDao.saveOrUpdate(data);
        }
        catch (Exception e)
        {
            return  false;
        }

        return true;
    }

    @Override
    public List<Map<String, Object>> list(String sqlStr, Object... params) {
        try {
            return itCrmCarInvoiceDao.list(sqlStr,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 自动匹配销售单里的财务未入库的车
     * @param uid
     * @param carId
     * @throws Exception
     */
    @Override
    public void refreshCar(String uid, String carId) throws Exception{

        String cid = TokenUtil.getCompanyId(uid);
        //查询车辆
        Map car = this.itCrmCarInvoiceDao.info("select * from t_scm_Car where id =? and finance_type in ('2','3')",carId);
        //查询销售单里是否有该车
        Map sale = this.itCrmCarInvoiceDao.info("select * from t_scm_sale_order where vin=? and car_id is null and company_id =? and status = '0'",car.get("vin"),cid);
        Map saleDetail = this.itCrmCarInvoiceDao.info("select * from t_scm_sale_order_detail where vin=? and car_id is null and company_id =? and status = '0'",car.get("vin"),cid);
        if(sale.size()>0){//有车

            TScmSaleOrder entity = new TScmSaleOrder();
            TScmSaleOrderDetail tScmSaleOrderDetail = new TScmSaleOrderDetail();
            TScmSaleOrder tt = (TScmSaleOrder) this.iHibernateDao.findEntity(TScmSaleOrder.class,sale.get("id").toString());
            entity.setId(tt.getId());
            TScmSaleOrderDetail td = (TScmSaleOrderDetail) this.iHibernateDao.findEntity(TScmSaleOrderDetail.class,saleDetail.get("id").toString());
            tScmSaleOrderDetail.setId(td.getId());
            //车系id
            if (null != car.get("carSeriesId")) {
                entity.setCarSeriesId(car.get("carSeriesId").toString());
                tScmSaleOrderDetail.setCarSeriesId(car.get("carSeriesId").toString());
            }
            //车型id
            if (null != car.get("carModelId")) {
                entity.setCarModelId(car.get("carModelId").toString());
                tScmSaleOrderDetail.setCarModelId(car.get("carModelId").toString());
            }
            //配置id
            if (null != car.get("configureId")) {
                entity.setConfigureId(car.get("configureId").toString());
                tScmSaleOrderDetail.setConfigureId(car.get("configureId").toString());
            }
            //外观颜色
            if (null != car.get("appearanceCode")) {
                entity.setAppearanceCode(car.get("appearanceCode").toString());
                tScmSaleOrderDetail.setAppearanceCode(car.get("appearanceCode").toString());
            }
            //内饰编号
            if (null != car.get("trimCode")) {
                entity.setTrimCode(car.get("trimCode").toString());
                tScmSaleOrderDetail.setTrimCode(car.get("trimCode").toString());
            }

            //初次入库时间
            if (null != car.get("enterWarehouseDateStart")) {
                entity.setFirstEnterWarehouseDate(car.get("enterWarehouseDateStart").toString());
                tScmSaleOrderDetail.setFirstEnterWarehouseDate(car.get("enterWarehouseDateStart").toString());
            }

            //车辆id
            entity.setCarId(car.get("id").toString());
            tScmSaleOrderDetail.setCarId(car.get("id").toString());
            tScmSaleOrderDetail.setStatus("0");
            entity.setSaleStatus("0");
            entity.setStatus("0");

            this.itCrmCarInvoiceDao.executeSQL("update t_scm_car set sale_type ='2' where id =?",carId);
            this.itCrmCarInvoiceDao.saveOrUpdate(entity);
            this.itCrmCarInvoiceDao.saveOrUpdate(tScmSaleOrderDetail);
        }



    }
}

